package edu.yctc.patterns.controller.rest.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.patterns.constant.ResultCode;
import edu.yctc.patterns.controller.rest.BlogRestController;
import edu.yctc.patterns.entity.BlogDO;
import edu.yctc.patterns.entity.ResultDO;
import edu.yctc.patterns.service.BlogIService;
import edu.yctc.patterns.vo.BlogVO;

/**
 * @className BlogRestControllerImpl
 * @description blogRestController
 * @author zyj
 * @date 2019年12月10日
 *
 */
@RestController
@ComponentScan({"edu.yctc.patterns.service"})
@RequestMapping("/restblog")
public class BlogRestControllerImpl implements BlogRestController {

    @Autowired
    private BlogIService blogService;

    @Override
    @PostMapping("get-blogvo-by-title")
    public ResultDO<BlogVO> getBlogVOByTitle(@RequestBody String title) {
        if (StringUtils.isBlank(title)) {
            return new ResultDO<BlogVO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        ResultDO<BlogDO> resultDO = blogService.getBlogDOByTitle(title);
        if (resultDO.isSuccess() == false) {
            return new ResultDO<BlogVO>(false, resultDO.getCode(), resultDO.getMsg(), null);
        }
        // 构建VO
        BlogVO blogVO = new BlogVO();
        BlogDO blogDO = resultDO.getModule();
        blogVO.setId(blogDO.getId());
        blogVO.setTitle(blogDO.getTitle());
        blogVO.setContent(blogDO.getContent());
        blogVO.setCreateTime(blogDO.getCreateTime());
        blogVO.setModifyTime(blogDO.getModifyTime());
        return new ResultDO<BlogVO>(true, resultDO.getCode(), resultDO.getMsg(), blogVO);
    }

    @Override
    @PostMapping("refresh-blog-cache")
    public ResultDO<Void> refreshBlogCache(@RequestBody String title) {
        if (StringUtils.isBlank(title)) {
            return new ResultDO<Void>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return blogService.refreshBlogCache(title);
    }

}
